package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    ShoppingCartMapper shoppingCartMapper;
    @Autowired
    DishMapper dishMapper;
    @Autowired
    SetmealMapper setmealMapper;
    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> getShoppingList() {
        //优化：如果管理端更改菜品价格，购物车的数据也应该更新
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getShoppingCartList(shoppingCart);
        return shoppingCarts;
    }

    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //设置当前用户的 用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());

        //查找表中数据，判断是否存在
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.getShoppingCartList(shoppingCart);

        //如果只存在一条数据，则直接 number+1
        if(shoppingCarts != null && shoppingCarts.size() == 1){
            for (ShoppingCart cart : shoppingCarts) {
                cart.setNumber(cart.getNumber() + 1);
                shoppingCartMapper.addNumber(cart);
            }
        }else{
            //若表中没有数据，则需要重新添加
            Long dishId = shoppingCartDTO.getDishId();
            //判断当前添加的是菜品还是套餐
            if(dishId != null){
                //获取当前菜品的其他基础信息（冗余字段，用来缓存数据，以免再次查表）
                Dish dish = dishMapper.getDishById(dishId);
                //将获取的字段赋值给购物车菜品

                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setName(dish.getName());
            }else{
                //获取套餐基础信息
                Setmeal setmeal = setmealMapper.getBySetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);

        }


    }

    @Override
    public void subOne(ShoppingCartDTO shoppingCartDTO) {
        //删除购物车单个菜品
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());


        //获取当前需要删除的菜品
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.getShoppingCartList(shoppingCart);
        ShoppingCart cart = shoppingCartList.get(0);
        //当菜品的数量sub到 0需要从表中删除该菜品
        if(cart.getNumber() - 1 == 0){
            shoppingCartMapper.deleteOne(cart);
        }
        cart.setNumber(cart.getNumber() - 1);

        shoppingCartMapper.addNumber(cart);

    }

    @Override
    public void clean() {
        //删除该用户的购物车
        Long userId = BaseContext.getCurrentId();

        shoppingCartMapper.deleteByUserId(userId);

    }
}
